/**
 * 
 */
package com.eshore.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.eshore.R;
import com.eshore.player.ShowRtmpPlayer;
import com.eshore.server.DBHelper;

public class FileListActivity extends Activity {

	protected ListView lv;
	protected int nCurrentIndex = -1;
	protected SimpleAdapter listItemAdapter;
	protected ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();

	// protected List<String> listItem = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try{
			this.setContentView(R.layout.filelist);
			lv = (ListView) findViewById(R.id.filelist);
			Cursor c = DBHelper.sqldb.rawQuery("select * from "
					+ DBHelper.TABLE_NAME + " where " + DBHelper.KEY_REMARKS
					+ "=''", null);
			final int dateidx = c.getColumnIndexOrThrow(DBHelper.KEY_DATE);
			final int cnidx = c.getColumnIndexOrThrow(DBHelper.KEY_COURSES);
			final int cididx = c.getColumnIndexOrThrow(DBHelper.KEY_CLASS_PATH);
			for (c.moveToFirst(); !(c.isAfterLast()); c.moveToNext()) {
				HashMap<String, String> item = new HashMap<String, String>();
				item.put("itemtext", c.getString(cnidx));
				item.put("stime", c.getString(dateidx));
				item.put("fpath", c.getString(cididx));
				listItem.add(item);
			}
			c.close();
			listItemAdapter = new SimpleAdapter(this,
					listItem,
					R.layout.item_text,
					new String[] { "itemtext", "stime", "fpath" }, 
					new int[] { R.id.itemtext,R.id.stime, R.id.fpath });
			lv.setAdapter(listItemAdapter);
			lv.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					lv.requestFocusFromTouch();
					lv.setSelection((int) arg3);
					nCurrentIndex = (int) arg3;
	
					finish();
					if(ShowRtmpPlayer._this_!=null)
						ShowRtmpPlayer._this_.finish();
					Intent intent = new Intent();
					intent.setClass(FileListActivity.this, ShowRtmpPlayer.class);
					HashMap<String, String> item = listItem.get(nCurrentIndex);
					intent.putExtra("filename", "/data/data/com.eshore/files/" + item.get("fpath"));
					FileListActivity.this.startActivity(intent);
				}
			});
		}catch(Exception e){
			
		}
	}

}
